package oop.data;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oop.db.dao.QuestionDAO;
import oop.db.dao.TestDAO;

public class Section {

	private long id;
	private String text;
	private int order;
	private long testId;
	private Map<Long, Question> questionMap = null;
	private List<Question> questions = null;

	public Section(String text, int order) {
		this(0, text, order, 0);
	}

	public Section(long id, String text, int order, long testId) {
		this.id = id;
		this.text = text;
		this.order = order;
		this.testId = testId;
	}

	public Map<Long, Question> getQuestionById() throws SQLException {
		if (questionMap == null) {
			questionMap = new HashMap<Long, Question>();
			for (Question q : getQuestions()) {
				questionMap.put(q.getId(), q);
			}
		}
		return questionMap;
	}

	public List<Question> getQuestions() throws SQLException {
		if (questions == null) {
			questions = QuestionDAO.fetchBySection(id);
		}
		return questions;
	}

	public long getId() {
		return id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	void setTest(Test test) {
		this.testId = test.getId();
	}

	public Test getTest() throws SQLException {
		return TestDAO.fetchById(testId);
	}

}
